<template>
  <div class="songlist-detail">
    <el-row :gutter="40">
      <el-col :span="6">
        <el-image
          :src="playlist.coverUrl"
          fit="cover"
          class="cover-image"
          style="border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);" />
      </el-col>

      <el-col :span="18">
        <div class="info-section">
          <h2 class="title">{{ playlist.title }}</h2>
          <el-tag type="success" class="tag">标签：{{ playlist.style }}</el-tag>
          <p class="introduction">介绍：{{ playlist.introduction }}</p>
        </div>
      </el-col>
    </el-row>

    <!-- 歌曲列表 -->
    <div class="songs-section">
      <h3 class="section-title">歌曲列表</h3>
      <el-table :data="songs" stripe border style="width: 100%; margin-bottom: 20px;">
        <el-table-column label="编号" width="80">
            <template #default="{ row }">
                <el-link  @click="songStore.addSongToPlaylist(row.id)">{{ row.id }}</el-link>
            </template>
        </el-table-column>
        <el-table-column prop="name" label="歌名" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button
              type="danger"
              size="small"
              @click="deleteSong(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script setup>

import { useSongStore } from '@/stores/song'

// 音乐播放器
const songStore = useSongStore()
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import {
  getSongListById,
  getSongListSongs,
  deleteSongFromList,
  addSongToList,
  getAllSongs
} from '@/api/song-list'
import { baseURL } from '@/api/request'
import { ElMessage, ElMessageBox } from 'element-plus'

const route = useRoute()
const playlist = ref({})
const songs = ref([])
const allSongs = ref([])
const selectedSongId = ref(null)

const fetchDetail = async () => {
  const response = await getSongListById(route.params.id)
  if (response.code === '0') {
    const data = response.data
    playlist.value = {
      id: data.id,
      title: data.title,
      coverUrl: data.pic ? baseURL + '/r' + data.pic : '/default/cover.jpg',
      style: data.style,
      introduction: data.introduction
    }
  }

  const resSongs = await getSongListSongs(route.params.id)
  if (resSongs.code === '0') {
    songs.value = resSongs.data
  }

  const resAll = await getAllSongs()
  if (resAll.code === '0') {
    allSongs.value = resAll.data
  }
}

onMounted(() => {
  fetchDetail()
})

const deleteSong = async (songId) => {
  try {
    await ElMessageBox.confirm('确认删除该歌曲？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    const res = await deleteSongFromList(route.params.id, songId)
    if (res.code === '0') {
      ElMessage.success('删除成功')
      fetchDetail()
    } else {
      ElMessage.error(res.msg || '删除失败')
    }
  } catch (err) {
    console.error('删除失败:', err)
  }
}

</script>

<style scoped>
.songlist-detail {
  padding: 30px;
}

.cover-image {
  width: 100%;
  height: auto;
  border-radius: 10px;
}

.title {
  font-size: 26px;
  font-weight: bold;
  margin-bottom: 10px;
  color: #333;
}

.tag {
  margin-bottom: 10px;
  font-size: 14px;
}

.introduction {
  font-size: 14px;
  color: #666;
  margin-top: 10px;
}

.songs-section {
  margin-top: 40px;
}

.section-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 15px;
  color: #409EFF;
  border-left: 4px solid #409EFF;
  padding-left: 10px;
}

.add-song-form {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 20px;
}
</style>
